package com.itheima.reggie.mapper;

import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DishMapper {
    @Select("select count(*)from dish where category_id#={categoryId}and is_deleted=0")
    Integer countByCid(Long categoryId);


    //插入菜品数据
    @Insert("insert into  dish values(null,#{name},#{categoryId},#{price},#{code}," +
            "#{image},#{description},#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")

    //默认情况下有主键但是disDtode的属性id为nULL
    //目标,执行插入后数据循环生成ID数据 返回设置的disDto对象id的值
    //解决方法 @Options
    //useGeneratedKeys = true获取生成的主键ID
    //keyColumn = "id",keyProperty = "id"
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    Integer insert(DishDto dishDto);

    //分页查询
    List<DishDto> findAll2(@Param("name") String name);

    //根据Id查询菜品
    DishDto findById(Long id);
    @Select("select *from dish where category_id = #{categoryId} and is_deleted=0 order by sort asc")
    //@Select("select * from dish where category_id = #{categoryId} and is_deleted=0 order by sort asc")
    List<Dish>findByCategoryId(Long categoryId);

    //根据类别ID和状态查询菜品列表
    @Select("select *from dish where category_id =#{categoryId} and is_deleted=0 and status=#{status} " +
            "order by sort asc" )

    List<Dish> findByCategoryIdAndStatus(@Param("categoryId") Long categoryId,
                                         @Param("status") Integer status);




}
